package com.fenxiangle.yueding.feature.focus.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.FoucsBo;
import com.fenxiangle.yueding.entity.bo.FoucsListBo;
import com.fenxiangle.yueding.entity.bo.HomeDataBo;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.feature.focus.contract.FocusContract;
import com.fenxiangle.yueding.feature.focus.dependencies.order.DaggerFocusComponent;
import com.fenxiangle.yueding.feature.focus.dependencies.order.FocusPresenterModule;
import com.fenxiangle.yueding.feature.focus.view.adapter.FocusAdapter;
import com.fenxiangle.yueding.feature.home.view.UserInfoActivity;
import com.fenxiangle.yueding.feature.mine.view.VideoPlayerActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.suozhang.framework.widget.SpacesItemDecoration;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.http.POST;


public class FocusFragment extends BaseFragment implements FocusContract.View, BaseQuickAdapter.OnItemClickListener, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.rv_foucs)
    RecyclerView rvFoucs;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    Unbinder unbinder;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<FoucsListBo> mData;
    private FocusAdapter mAdapter;

    @Inject
    FocusContract.Presenter mPresenter;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_focus;
    }

    @Override
    protected void initInjector() {
        DaggerFocusComponent.builder().focusPresenterModule(new FocusPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        initAdapter();

    }

    public void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(rvFoucs);
        rvFoucs.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvFoucs.addItemDecoration(new SpacesItemDecoration(2, 16, false));
        mAdapter = new FocusAdapter();
        mAdapter.bindToRecyclerView(rvFoucs);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.setOnLoadMoreListener(this, rvFoucs);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }


    @Override
    protected void initData() {
        pageIndex = 1;
        mPresenter.getOrderList(pageIndex, pageSize);
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void showOrderListSuccess(List<FoucsListBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showOrderListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showOrderListMoreSuccess(List<FoucsListBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showOrderListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showOrderListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    private void refresh() {
        mAdapter.setEnableLoadMore(false);
        initData();
    }

    private void loadMore() {
        mPresenter.getOrderListMore(pageIndex, pageSize);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_fouce_cover:
                if (!TextUtils.isEmpty(mData.get(position).getVideoUrl()) && !TextUtils.isEmpty(mData.get(position).getVideoCover())) {
                    FoucsListBo homeDataBo=  mData.get(position);
                    UserCenterBo userCenterBo=new UserCenterBo();
                    userCenterBo.setFans(homeDataBo.getFans());
                    userCenterBo.setCreditScore(homeDataBo.getCreditScore());
                    userCenterBo.setTimeValue(homeDataBo.getTimeValue());

                    Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
                    intent.putExtra("userCenterBo", userCenterBo);
                    intent.putExtra("videoCoverImg",mData.get(position).getVideoCover());
                    intent.putExtra("videoPath",mData.get(position).getVideoUrl());
                    intent.putExtra("dec", mData.get(position).getVideoDescribe());
                    startActivity(intent);
                }else {
                    Intent intents = new Intent(getActivity(), UserInfoActivity.class);
                    intents.putExtra("id", mData.get(position).getUserUid());
                    startActivity(intents);
                }
                break;

            case R.id.iv_foucs_head:
                Intent intents = new Intent(getActivity(), UserInfoActivity.class);
                intents.putExtra("id", mData.get(position).getUserUid());
                startActivity(intents);
                break;
        }
    }
}
